package com.yzh.openai.trigger.job;

import com.google.common.eventbus.EventBus;
import com.yzh.openai.domain.order.service.IOrderService;
import com.yzh.openai.trigger.mq.producer.RedisPublisher;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 补货任务
 */
@Slf4j
@Component
public class OrderReplenishmentJob {
    @Resource
    private IOrderService orderService;
    
    @Resource
    private RedisPublisher publisher;

    /**
     * 执行补货任务，每隔30秒执行一次：超时1分钟，已支付，未发货的订单
     */
    @Scheduled(cron = "0/30 * * * * ? ")
    public void exec() {
        try {
            List<String> orderIdList = orderService.queryReplenishmentOrder();
            if (orderIdList.isEmpty()) {
                log.info("定时任务【补货】未查询到需要补货的订单，无需执行");
                return;
            }
            for (String orderId : orderIdList) {
                publisher.publishOrder(orderId);
                log.info("定时任务【补货】成功，orderId={}", orderId);
            }
        } catch (Exception e) {
            log.error("定时任务【补货】失败，出现异常", e);
        }
    }
}
